
PLUG.Platform = PLUG_PLATFORM_CLIENT

PLUG.ClassName = "cl_groupcpanel"
PLUG.PrintName = "Usergroup Control Panel"
PLUG.Author = "thomasfn"

PLUG.Hard = true

local GrpTabs = {
	general = { "General Settings", "gui/silkicons/user" },
	access = { "Access Management", "gui/silkicons/key" },
}
function PLUG:GetGroupTabs()
	return GrpTabs
end

function PLUG:BuildGroupTab( id, panel, grp )
	local w, h = panel:GetSize()
	
	if (id == "general") then
		plugs.Call( "CreateLabel2", "Manage Usergroup", 10, 10, panel )
		local nw = plugs.Call( "CreateLabel2", "Usergroup Name: " .. grp.PrintName, 10, 30, panel ):GetWide()
		
		plugs.Call( "CreateApplyEntry", "Rank", grp.Rank, 10, 50, w*0.1, w*0.1, 20, panel, function( self )
			plugs.Call( "SendCommand", "grp_setrank", grp.ClassName, self:GetTXT() )
		end )
		
		local propn = vgui.Create( "DPanel", panel )
		propn:SetPos( 10, 80 )
		propn:SetSize( w*0.3 - 20, h - 120 )
		local bgcol = Color( 100, 100, 100 )
		function propn:Paint()
			draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), bgcol )
		end
		plugs.Call( "BuildGroupAutoPromote", grp, propn )
		
		local btn = vgui.Create( "DButton", panel )
		btn:SetPos( 10, h-30 )
		btn:SetSize( w*0.3 - 20, 20 )
		btn:SetText( "Delete" )
		function btn:DoClick()
			local q = "Are you sure you want to delete that usergroup?"
			plugs.Call( "PromptYesNo", q, function() plugs.Call( "SendCommand", "grp_delete", grp.ClassName ) end )
		end
		
		local colpn = vgui.Create( "DPanel", panel )
		colpn:SetPos( w*0.3, 10 )
		colpn:SetSize( w*0.7 - 10, h - 20 )
		function colpn:Paint()
			draw.RoundedBox( 6, 0, 0, self:GetWide(), self:GetTall(), bgcol )
		end
		plugs.Call( "BuildGroupColourSelect", grp, colpn )
		
	end
	if (id == "access") then
		local function Paint( panel )
			surface.SetDrawColor( 75, 75, 75, 255 )
			surface.DrawRect( 0, 0, panel:GetWide(), panel:GetTall() )
		end
	
		plugs.Call( "CreateLabel", "Group Allowed Entities", w*0.25, 10, panel )
		local cla = plugs.Call( "CreateSENTChecklist", panel, 10, 30, w*0.5 - 15, h-60, grp.Rstrict )
		cla.Paint = Paint
		local btn = vgui.Create( "DButton", panel )
		btn:SetPos( 10, h-25 )
		btn:SetSize( w*0.5 - 15, 20 )
		btn:SetText( "Apply" )
		function btn:DoClick()
			local changed = cla.Changed
			// PrintTable( changed )
			if (table.Count( changed ) > 0) then
				local tosend = { "applygrouprstrict", grp.ClassName, changed }
				datastream.StreamToServer( "gmanage_sv", tosend )
				// PrintTable( tosend )
			end
		end
		
		plugs.Call( "CreateLabel", "Group Privileges", w*0.75, 10, panel )
		local clb = plugs.Call( "CreatePrivilegeChecklist", panel, w*0.5 + 5, 30, w*0.5 - 15, h-60, grp.Privs )
		clb.Paint = Paint
		local btn = vgui.Create( "DButton", panel )
		btn:SetPos( w*0.5 + 5, h-25 )
		btn:SetSize( w*0.5 - 15, 20 )
		btn:SetText( "Apply" )
		function btn:DoClick()
			local changed = clb.Changed
			// PrintTable( changed )
			if (table.Count( changed ) > 0) then
				local tosend = { "applygroupprivs", grp.ClassName, changed }
				datastream.StreamToServer( "gmanage_sv", tosend )
				// PrintTable( tosend )
			end
		end
	end
end

function PLUG:BuildGroupAutoPromote( grp, pn )
	local lbl = vgui.Create( "DLabel", pn )
	lbl:SetText( "Auto Promote" )
	lbl:SizeToContents()
	lbl:SetPos( 5, 5 )
	
	local pnw = pn:GetWide()
	
	local xa = plugs.Call( "CreateLabel2", "To:", 5, 30, pn ):GetWide() + 15
	local xb = plugs.Call( "CreateLabel2", "After:", 5, 50, pn ):GetWide() + 15
	
	local txta = plugs.Call( "CreateTextbox", xa, 30, pn:GetWide()-xa-5, pn )
	local txtb = plugs.Call( "CreateTextbox", xb, 50, pn:GetWide()-xb-5, pn )
	
	txta:SetText( grp.AutoPromoteGroup or "" )
	txtb:SetText( grp.AutoPromoteTime or "" )
	
	local btn = vgui.Create( "DButton", pn )
	btn:SetPos( 5, 70 )
	btn:SetText( "Apply" )
	btn:SetSize( pn:GetWide()-10, 20 )
	function btn:DoClick()
		local ugname = txta:GetValue()
		if (ugname == "") then
			plugs.Call( "SendCommand", "grp_disableautopromote", grp.ClassName )
		else
			plugs.Call( "SendCommand", "grp_autopromote", grp.ClassName, ugname, txtb:GetValue() )
		end
	end
	
	plugs.Call( "CreateLabel", "Leave 'To' blank to disable.", pn:GetWide()*0.5, 100, pn )
	plugs.Call( "CreateLabel", "'After' is in minutes.", pn:GetWide()*0.5, 120, pn )
end

function PLUG:BuildGroupColourSelect( grp, pn )
	local col = vgui.Create( "DColorMixer", pn )
	col:SetPos( 5, 5 )
	col:SetColor( grp.Colour or Color( 255, 255, 255 ) )
	col:SetWide( pn:GetWide() - 10 )
	col:SetTall( pn:GetTall() - 35 )
	col:PerformLayout()
	col:CenterHorizontal()
	
	local btna = vgui.Create( "DButton", pn )
	btna:SetText( "Apply Colour" )
	btna:SetPos( 5, pn:GetTall() - 25 )
	btna:SetSize( pn:GetWide()*0.5 - 10, 20 )
	function btna:DoClick()
		local c = col:GetColor()
		plugs.Call( "SendCommand", "grp_setcol", grp.ClassName, c.r, c.g, c.b, c.a )
	end
	
	local btnb = vgui.Create( "DButton", pn )
	btnb:SetText( "Remove Colour" )
	btnb:SetPos( pn:GetWide()*0.5 + 5, pn:GetTall() - 25 )
	btnb:SetSize( pn:GetWide()*0.5 - 10, 20 )
	function btnb:DoClick()
		plugs.Call( "SendCommand", "grp_setcol", grp.ClassName, "none" )
	end
end